package com.yuju.account.service.impl;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.account.dao.IAccountDao;
import com.yuju.account.dao.entity.Account;
import com.yuju.account.enums.AccountReturnCodeEnum;
import com.yuju.account.enums.AccountStatusEnum;
import com.yuju.account.enums.AccountTypeEnum;
import com.yuju.account.enums.BusinessTypeDetailEnum;
import com.yuju.account.enums.BusinessTypeEnum;
import com.yuju.account.enums.ReserveAccountReturnCodeEnum;
import com.yuju.account.enums.RoomOrderStatusEnum;
import com.yuju.account.enums.SettleAccountReturnCodeEnum;
import com.yuju.account.enums.UnitAccountReturnCodeEnum;
import com.yuju.account.enums.UnitAccountSettleEnum;
import com.yuju.account.model.OrderAccountStatModel;
import com.yuju.account.model.RoomOrderModel;
import com.yuju.account.model.UnitAccountModel;
import com.yuju.account.service.IAccountService;
import com.yuju.account.service.IPayService;
import com.yuju.account.service.IShiftService;
import com.yuju.account.service.IUnitAccountService;
import com.yuju.common.PageModel;
import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.enums.CheckinTypeEnum;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.exception.BusinessException;
import com.yuju.group.service.ICodeGenService;
import com.yuju.hotel.enums.CustomersEnum;
import com.yuju.hotel.model.CheckinStandardModel;
import com.yuju.hotel.model.RoomTypeModel;
import com.yuju.hotel.service.ICheckinStandardService;
import com.yuju.hotel.service.IRoomTypeService;
import com.yuju.model.account.AccountModel;
import com.yuju.util.DateUtil;
import com.yuju.util.ExcelExp;
import com.yuju.util.Utils;

/**
 * 类名：AccountServiceImpl  <br />
 *
 * 功能：
 *
 * @author : jiaokn <br />
 * @Date : 2017年11月27日 上午9:50:03  <br />
 * @version : 1.1 <br />
 */
@Service
public class AccountServiceImpl extends AbstractBaseService<AccountModel, Account, String> implements IAccountService {
	@Autowired
	private IPayService payService;
	
	@Autowired
	private IAccountDao accountDao;
	
	@Autowired
	private ICodeGenService codeGenService;
	
	@Autowired
	private IUnitAccountService unitAccountService;
	
	@Autowired
	private IShiftService shiftService;
	
	@Autowired
	private ICheckinStandardService checkinStandardService;
	
	@Autowired
	private IRoomTypeService roomTypeService;
	
	@Override
	public IBaseDao<AccountModel, Account, String> getDao() {
		// TODO Auto-generated method stub
		return accountDao;
	}
	
	
	public String saveGetId(AccountModel accountModel){
		// 设置编码
		accountModel.setAccountCode(codeGenService.generate(accountModel.getGroupId(), accountModel.getHotelId(),
				accountModel.getHotelCode(), BusinessTypeEnum.getType(accountModel.getBusinessType())));

		if (Utils.isNull(accountModel.getAccountCode())) {
			throw new BusinessException(ReturnCodeEnum.ORDER_NO_GEN_FALURE.getCode(),
					ReturnCodeEnum.ORDER_NO_GEN_FALURE.getDesc());
		}
		
		accountModel.setClassId(shiftService.getCurrentClassId(accountModel.getHotelId()));
		accountModel.setId(Utils.getUUID());
		if(accountModel.getBusinessType() == BusinessTypeEnum.BUSINESS_TYPE_YSFF.getValue()){
			Date date = DateUtil.parseDate(new Date(), "yyyy-MM-dd");
			accountModel.setGmtCreate(new Date(date.getTime() - 1000));
			accountModel.setGmtModified(new Date());
		}else{
			accountModel.setGmtCreate(new Date());
			accountModel.setGmtModified(new Date());
		}
		if(super.save(accountModel)<1){
			return null;
		}
		return accountModel.getId();
	}
	
	/**
	 * 功能:预订账务新增接口 <br/>
	 * 
	 * 1.根据账务状态、类型、营业类型判断要更新的金额是加还是减
	 * 2.更新预订单中预订金的金额
	 * 3.新增账务
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月4日 上午11:30:18<br/>
	 * 
	 * @param accountModel
	 * @return <br/>
	 * @see com.yuju.account.service.IAccountService#insertDeposit(com.yuju.account.model.AccountModel)
	 */
	public String saveReserveAccount(AccountModel accountModel){
		BigDecimal money = new BigDecimal(0);
		if (accountModel.getAccountStatus() == AccountStatusEnum.STATUS_OK.getValue()) {
			if (accountModel.getAccountType() == AccountTypeEnum.ACCOUNT_TYPE_SS.getValue()) {
				if (accountModel.getBusinessType() == BusinessTypeEnum.BUSINESS_TYPE_DJ.getValue()
						|| accountModel.getBusinessType() == BusinessTypeEnum.BUSINESS_TYPE_SK.getValue()) {
					money = accountModel.getMoney();
				} else {
					money = accountModel.getMoney().multiply(new BigDecimal(-1));
				}
			}
		} else if (accountModel.getAccountStatus() == AccountStatusEnum.STATUS_GZ.getValue()) {
			money = accountModel.getMoney();
		} else {
			throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_STATUS_NOMATCH.getCode(),
					AccountReturnCodeEnum.ACCOUNT_STATUS_NOMATCH.getDesc());
		}
		//更新预订单中预订金的金额
		Map<String, Object> param = new HashMap<>();
		param.put("id", accountModel.getReserveId());
		param.put("money", money);
		param.put("gmtModified", new Date());
		if (accountDao.updateBalanceByReserveId(param) < 1) {
			throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_DESPOITYUPDATE_FAILURE.getCode(),
					AccountReturnCodeEnum.ACCOUNT_DESPOITYUPDATE_FAILURE.getDesc());
		}
		
		return this.saveGetId(accountModel);
		
	}

	/**
	 * 功能:预订账务更新接口 <br/>
	 * 
	 * 1.根据账务状态、类型、营业类型判断要更新的金额是加还是减
	 * 2.更新预订单中预订金的金额
	 * 3.新增账务
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月4日 下午8:01:25<br/>
	 * 
	 * @param accountModel
	 * @return <br/>
	 * @see com.yuju.account.service.IAccountService#updateReserveAccount(com.yuju.account.model.AccountModel)
	 */
	@Override
	public int updateReserveAccount(AccountModel accountModel) {
		BigDecimal money = new BigDecimal(0);
		if(accountModel.getAccountStatus() == AccountStatusEnum.STATUS_CANCEL.getValue()){
			if(accountModel.getAccountType() == AccountTypeEnum.ACCOUNT_TYPE_SS.getValue()){
				if(accountModel.getBusinessType() == BusinessTypeEnum.BUSINESS_TYPE_DJ.getValue()){
					money = accountModel.getMoney().multiply(new BigDecimal(-1));
				}else{
					money = accountModel.getMoney();
				}
			}
		}else{
			throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_STATUS_NOMATCH.getCode(),
					AccountReturnCodeEnum.ACCOUNT_STATUS_NOMATCH.getDesc());
		}
		//更新预订单中预订金的金额
		Map<String, Object> param = new HashMap<>();
		param.put("id", accountModel.getReserveId());
		param.put("money", money);
		param.put("gmtModified", new Date());
		if (accountDao.updateBalanceByReserveId(param) < 1) {
			throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_DESPOITYUPDATE_FAILURE.getCode(),
					AccountReturnCodeEnum.ACCOUNT_DESPOITYUPDATE_FAILURE.getDesc());
		}
		return this.updateSelective(accountModel);
	}

	/**
	 * 功能:订单账务新增接口 <br/>
	 * 
	 * 1.根据账务状态、类型、营业类型判断要更新的金额是加还是减
	 * 2.更新订单中的余额
	 * 3.更新客账的余额
	 * 4.新增账务
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月4日 下午8:05:51<br/>
	 * 
	 * @param accountModel
	 * @return <br/>
	 * @see com.yuju.account.service.IAccountService#insertOrderAccount(com.yuju.account.model.AccountModel)
	 */
	@Override
	public String saveOrderAccount(AccountModel accountModel) {
		BigDecimal money = new BigDecimal(0);
		if(accountModel.getAccountStatus() == AccountStatusEnum.STATUS_OK.getValue()){
			if(accountModel.getAccountType() == AccountTypeEnum.ACCOUNT_TYPE_YS.getValue()){
				money = accountModel.getMoney().multiply(new BigDecimal(-1));
			}else{
				if(accountModel.getBusinessType() == BusinessTypeEnum.BUSINESS_TYPE_TK.getValue()){
					money = accountModel.getMoney().multiply(new BigDecimal(-1));
				}else{
					money = accountModel.getMoney();
				}
			}
		}else if(accountModel.getAccountStatus() == AccountStatusEnum.STATUS_GZ.getValue()){
			money = accountModel.getMoney();
		}else{
			throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_STATUS_NOMATCH.getCode(),
					AccountReturnCodeEnum.ACCOUNT_STATUS_NOMATCH.getDesc());
		}
		
		// 更新订单表的余额
		if (Utils.isNotNull(accountModel.getRoomOrderId())) {
			Map<String, Object> tradeOrderMap = new HashMap<>();
			tradeOrderMap.put("id", accountModel.getRoomOrderId());
			tradeOrderMap.put("money", money);
			tradeOrderMap.put("gmtModified", new Date());
			tradeOrderMap.put("updateUserId", accountModel.getCreateUserId());
			if(accountDao.updateBalanceByOrderId(tradeOrderMap)<1){
				throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_ORDERBALANCEUPDATE_FAILURE.getCode(),
						AccountReturnCodeEnum.ACCOUNT_ORDERBALANCEUPDATE_FAILURE.getDesc());
			}
		}

		// 更新客账表的余额
		if (Utils.isNotNull(accountModel.getRoomGuestId())) {
			Map<String, Object> tradeRoomGuestMap = new HashMap<>();
			tradeRoomGuestMap.put("id", accountModel.getRoomGuestId());
			tradeRoomGuestMap.put("money", money);
			tradeRoomGuestMap.put("gmtModified", new Date());
			if(accountDao.updateBalanceByRoomGuestId(tradeRoomGuestMap)<1){
				throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_ROOMBALANCEUPDATE_FAILURE.getCode(),
						AccountReturnCodeEnum.ACCOUNT_ROOMBALANCEUPDATE_FAILURE.getDesc());
			}
		}
		
		String accountId = this.saveGetId(accountModel);
		
		// 支付宝微信：支付&退款
		if (BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_WX.getValue() == accountModel.getBusinessTypeDetail()
				|| BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_ZFB.getValue() == accountModel.getBusinessTypeDetail()) {
			payService.pay(accountModel);
		} else if (BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_WXTK.getValue() == accountModel.getBusinessTypeDetail()
				|| BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_ZFBTK.getValue() == accountModel.getBusinessTypeDetail()) {
			payService.refund(accountModel);
		}
		
		return accountId;
	}

	/**
	 * 功能:订单账务更新接口 <br/>
	 * 
	 * 1.根据账务状态、类型、营业类型判断要更新的金额是加还是减
	 * 2.更新订单中的余额
	 * 3.更新客账的余额
	 * 4.新增账务
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月4日 下午8:19:24<br/>
	 * 
	 * @param accountModel
	 * @return <br/>
	 * @see com.yuju.account.service.IAccountService#updateOrderAccount(com.yuju.account.model.AccountModel)
	 */
	@Override
	public int updateOrderAccount(AccountModel accountModel) {
		BigDecimal money = new BigDecimal(0);
		if (accountModel.getAccountStatus() == AccountStatusEnum.STATUS_OK.getValue()) {
			if (accountModel.getAccountType() == AccountTypeEnum.ACCOUNT_TYPE_YS.getValue()) {
					money = accountModel.getMoney();
			} else {
				if (accountModel.getBusinessType() == BusinessTypeEnum.BUSINESS_TYPE_TK.getValue()) {
					money = accountModel.getMoney();
				} else {
					money = accountModel.getMoney().multiply(new BigDecimal(-1));
				}
			}
		} else if (accountModel.getAccountStatus() == AccountStatusEnum.STATUS_GZ.getValue()) {
			money = accountModel.getMoney().multiply(new BigDecimal(-1));
		} else {
			throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_STATUS_NOMATCH.getCode(),
					AccountReturnCodeEnum.ACCOUNT_STATUS_NOMATCH.getDesc());
		}
		// 更新订单表的余额
		Map<String, Object> tradeOrderMap = new HashMap<>();
		tradeOrderMap.put("id", accountModel.getRoomOrderId());
		tradeOrderMap.put("money", money);
		tradeOrderMap.put("gmtModified", new Date());
		tradeOrderMap.put("updateUserId", accountModel.getUpdateUserId());
		if (accountDao.updateBalanceByOrderId(tradeOrderMap) < 1) {
			throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_ORDERBALANCEUPDATE_FAILURE.getCode(),
					AccountReturnCodeEnum.ACCOUNT_ORDERBALANCEUPDATE_FAILURE.getDesc());
		}

		// 更新客账表的余额
		Map<String, Object> tradeRoomGuestMap = new HashMap<>();
		tradeRoomGuestMap.put("id", accountModel.getRoomGuestId());
		tradeRoomGuestMap.put("money", money);
		tradeRoomGuestMap.put("gmtModified", new Date());
		if (accountDao.updateBalanceByRoomGuestId(tradeRoomGuestMap) < 1) {
			throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_ROOMBALANCEUPDATE_FAILURE.getCode(),
					AccountReturnCodeEnum.ACCOUNT_ROOMBALANCEUPDATE_FAILURE.getDesc());
		}

		accountModel.setAccountStatus(AccountStatusEnum.STATUS_CANCEL.getValue());
		accountModel.setAccountStatusName(AccountStatusEnum.STATUS_CANCEL.getName());
		return this.updateSelective(accountModel);
	}

	/**
	 * 功能：订金转押金<br/>
	 *
	 *1.根据预订单ID查询所有状态为正常的账务
	 *2.循环冲调这些账务并统计金额（按支付方式统计，订金为加，退款为减）
	 *3.根据订单ID和统计结果 (按支付方式)添加押金账务
	 *
	 * @author jiaokn
	 * @Date 2017年12月7日 下午7:38:25 <br/>
	 */
	@Override
	public int saveTransformAccount(String groupId, String hotelId, String hotelCode, String reserveId, String roomOrderId,
			String roomGuestId, String empId) {
		// 根据预订单ID查询状态为正常的账务
		AccountModel accountModelFind = new AccountModel();
		accountModelFind.setReserveId(reserveId);
		accountModelFind.setHotelId(hotelId);
		accountModelFind.setGroupId(groupId);
		accountModelFind.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
		List<AccountModel> list = this.list(accountModelFind);
		if (Utils.isNull(list) || list.size() == 0) {
			return 1;
//			throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_QUERY_FAILURE.getCode(),
//					AccountReturnCodeEnum.ACCOUNT_QUERY_FAILURE.getDesc());
		}
		// 循环冲调并统计金额
		BigDecimal sumXJ = new BigDecimal(0);
		BigDecimal sumZZ = new BigDecimal(0);
		BigDecimal sumWX = new BigDecimal(0);
		BigDecimal sumZFB = new BigDecimal(0);
		BigDecimal sumSK = new BigDecimal(0);

		for (AccountModel accountModel : list) {
			// 将原账务状态变为 取消
//			accountModel.setAccountStatus(AccountStatusEnum.STATUS_CANCEL.getValue());
//			accountModel.setAccountStatusName(AccountStatusEnum.STATUS_CANCEL.getName());
//			accountModel.setUpdateUserId(empId);
//			accountModel.setGmtModified(new Date());
			// 统计金额
			if (accountModel.getBusinessType() == BusinessTypeEnum.BUSINESS_TYPE_DJ.getValue()) {
				if (accountModel.getBusinessTypeDetail() == BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_XJ.getValue()) {
					sumXJ = sumXJ.add(accountModel.getMoney());
				} else if (accountModel.getBusinessTypeDetail() == BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_ZZ
						.getValue()) {
					sumZZ = sumZZ.add(accountModel.getMoney());
				} else if (accountModel.getBusinessTypeDetail() == BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_WX
						.getValue()) {
					sumWX = sumWX.add(accountModel.getMoney());
				} else if (accountModel.getBusinessTypeDetail() == BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_ZFB
						.getValue()) {
					sumZFB = sumZFB.add(accountModel.getMoney());
				} else {
					sumSK = sumSK.add(accountModel.getMoney());
				}
			} else {
				if (accountModel.getBusinessTypeDetail() == BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_XJ.getValue()) {
					sumXJ = sumXJ.subtract(accountModel.getMoney());
				} else if (accountModel.getBusinessTypeDetail() == BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_ZZ
						.getValue()) {
					sumZZ = sumZZ.subtract(accountModel.getMoney());
				} else if (accountModel.getBusinessTypeDetail() == BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_WX
						.getValue()) {
					sumWX = sumWX.subtract(accountModel.getMoney());
				} else if (accountModel.getBusinessTypeDetail() == BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_ZFB
						.getValue()) {
					sumZFB = sumZFB.subtract(accountModel.getMoney());
				} else {
					sumSK = sumSK.subtract(accountModel.getMoney());
				}
			}
			
			AccountModel accountModelCT = new AccountModel();
			accountModelCT.setHotelId(accountModel.getHotelId());
			accountModelCT.setGroupId(accountModel.getGroupId());
			accountModelCT.setHotelCode(accountModel.getHotelCode());
			accountModelCT.setReserveId(accountModel.getReserveId());
			accountModelCT.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
			accountModelCT.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
			accountModelCT.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue());
			accountModelCT.setBusinessType(accountModel.getBusinessType());
			accountModelCT.setBusinessTypeName(BusinessTypeEnum.getName(accountModel.getBusinessType()));
			accountModelCT.setBusinessTypeDetail(accountModel.getBusinessTypeDetail());
			accountModelCT
					.setBusinessTypeDetailName(BusinessTypeDetailEnum.getName(accountModel.getBusinessTypeDetail()));
			accountModelCT.setBusinessDetail("订金转押金");
			accountModelCT.setMoney(accountModel.getMoney().multiply(new BigDecimal(-1)));
			accountModelCT.setAmount(1);
			accountModelCT.setAccountCode(codeGenService.generate(accountModel.getGroupId(), accountModel.getHotelId(),
					accountModel.getHotelCode(), BusinessTypeEnum.getType(accountModel.getBusinessType())));
			accountModelCT.setCreateUserId(accountModel.getUpdateUserId());

			if (Utils.isNull(this.saveGetId(accountModelCT))) {
				throw new BusinessException(ReserveAccountReturnCodeEnum.RESERVEACCOUNT_SAVE_FAILURE.getCode(),
						ReserveAccountReturnCodeEnum.RESERVEACCOUNT_SAVE_FAILURE.getDesc());
			}
			
//			if (this.updateReserveAccount(accountModel) <= 0) {
//				throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_UPDATE_FAILURE.getCode(),
//						AccountReturnCodeEnum.ACCOUNT_UPDATE_FAILURE.getDesc());
//			}
		}

		// 根据订单Id和统计金额 添加 押金账务
		AccountModel accountModel = new AccountModel();
		accountModel.setGroupId(groupId);
		accountModel.setHotelId(hotelId);
		accountModel.setHotelCode(hotelCode);
		accountModel.setRoomOrderId(roomOrderId);
		accountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
		accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
		accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue());
		accountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_YJ.getValue());
		accountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_YJ.getName());
		accountModel.setRoomGuestId(roomGuestId);
		accountModel.setCreateUserId(empId);
		accountModel.setAmount(1);
		accountModel.setBusinessDetail("订金转押金");
		int i = 0;
		if (sumXJ.compareTo(BigDecimal.ZERO) == 1) {
			accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_XJ.getValue());
			accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_XJ.getName());
			accountModel.setMoney(sumXJ);
			if (Utils.isNull(this.saveOrderAccount(accountModel))) {
				return 0;
			}else{
				i++;
			}
		}
		if (sumZZ.compareTo(BigDecimal.ZERO) == 1) {
			accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_ZZ.getValue());
			accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_ZZ.getName());
			accountModel.setMoney(sumZZ);
			if (Utils.isNull(this.saveOrderAccount(accountModel))) {
				return 0;
			}else{
				i++;
			}
		}
		if (sumWX.compareTo(BigDecimal.ZERO) == 1) {
			accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_WX.getValue());
			accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_WX.getName());
			accountModel.setMoney(sumWX);
			if (Utils.isNull(this.saveOrderAccount(accountModel))) {
				return 0;
			}else{
				i++;
			}
		}
		if (sumZFB.compareTo(BigDecimal.ZERO) == 1) {
			accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_ZFB.getValue());
			accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_ZFB.getName());
			accountModel.setMoney(sumZFB);
			if (Utils.isNull(this.saveOrderAccount(accountModel))) {
				return 0;
			}else{
				i++;
			}
		}
		if (sumSK.compareTo(BigDecimal.ZERO) == 1) {
			accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_SK.getValue());
			accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_SK.getName());
			accountModel.setMoney(sumSK);
			if (Utils.isNull(this.saveOrderAccount(accountModel))) {
				return 0;
			}else{
				i++;
			}
		}

		return i;
	}

	/**
	 * 功能：新增冲调<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月8日 下午4:11:03 <br/>
	 */
	@Override
	public int saveCT(AccountModel accountModel) {
		AccountModel accountModelCT = new AccountModel();
		accountModelCT.setGroupId(accountModel.getGroupId());
		accountModelCT.setHotelId(accountModel.getHotelId());
		accountModelCT.setRoomOrderId(accountModel.getRoomOrderId());
		accountModelCT.setAccountStatus(AccountStatusEnum.STATUS_DEL.getValue());
		accountModelCT.setAccountStatusName(AccountStatusEnum.STATUS_DEL.getName());
		accountModelCT.setAccountType(accountModel.getAccountType());
		accountModelCT.setBusinessType(accountModel.getBusinessType());
		accountModelCT.setBusinessTypeName(BusinessTypeEnum.getName(accountModel.getBusinessType()));
		accountModelCT.setBusinessTypeDetail(accountModel.getBusinessTypeDetail());
		accountModelCT.setBusinessTypeDetailName(BusinessTypeDetailEnum.getName(accountModel.getBusinessTypeDetail()));
		accountModelCT.setBusinessDetail("冲调" + accountModel.getAccountCode());
		accountModelCT.setMoney(accountModel.getMoney().multiply(new BigDecimal(-1)));
		accountModelCT.setRoomGuestId(accountModel.getRoomGuestId());
		accountModelCT.setCreateUserId(accountModel.getCreateUserId());
		accountModelCT.setUpdateUserId(accountModel.getUpdateUserId());
		accountModelCT.setAmount(1);
		accountModelCT.setRemark(accountModel.getRemark());
		accountModelCT.setAccountCode(codeGenService.generate(accountModelCT.getGroupId(), accountModelCT.getHotelId(),
				accountModel.getHotelCode(), BusinessTypeEnum.getType(accountModelCT.getBusinessType())));
		if (Utils.isNull(this.saveGetId(accountModelCT))) {
			throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getCode(),
					AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getDesc());
		}
		
		accountModel.setRemark(null);
		return this.updateOrderAccount(accountModel);
	}

	/**
	 * 功能: 新增销售点<br/>
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月9日 下午5:54:14<br/>
	 * 
	 * @param map
	 * @return <br/>
	 * @see com.yuju.account.service.IAccountService#saveXSD(java.util.Map)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public int saveXSD(Map<String, Object> map) {
		ArrayList<AccountModel> accounts = (ArrayList<AccountModel>) map.get("accounts");
		AccountModel accountModel = (AccountModel) map.get("accountModel");

		int i = 0;
		for (AccountModel accountModelXSD : accounts) {
			accountModelXSD.setGroupId(accountModel.getGroupId());
			accountModelXSD.setHotelId(accountModel.getHotelId());
			accountModelXSD.setHotelCode(accountModel.getHotelCode());
			accountModelXSD.setRoomOrderId(accountModel.getRoomOrderId());
			accountModelXSD.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
			accountModelXSD.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_XSD.getValue());
			accountModelXSD.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_XSD.getName());
			accountModelXSD
					.setBusinessTypeDetailName(BusinessTypeDetailEnum.getName(accountModelXSD.getBusinessTypeDetail()));
			accountModelXSD.setRoomGuestId(accountModel.getRoomGuestId());
			accountModelXSD.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
			accountModelXSD.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
			accountModelXSD.setCreateUserId(accountModel.getCreateUserId());
			if (Utils.isNull(this.saveOrderAccount(accountModelXSD))) {
				throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getCode(),
						AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getDesc());
			} else {
				i++;
			}
		}

		// 付现记录存储
		if (Utils.isNotNull(accountModel.getMoney())) {
			accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue());
			accountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_SK.getValue());
			accountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_SK.getName());
			accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_XJ.getValue());
			accountModel
					.setBusinessTypeDetailName(BusinessTypeDetailEnum.getName(accountModel.getBusinessTypeDetail()));
			accountModel.setBusinessDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_XJ.getName()
					+ BusinessTypeEnum.BUSINESS_TYPE_SK.getName());
			accountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
			accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
			accountModel.setAmount(1);
			if ( Utils.isNull(this.saveOrderAccount(accountModel))) {
				throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getCode(),
						AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getDesc());
			}
		}

		return i;

	}

	/**
	 * 功能:新增挂账 <br/>
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月8日 下午4:46:36<br/>
	 * 
	 * @param map
	 * @return <br/>
	 * @see com.yuju.account.service.IAccountService#insertGZ(java.util.Map)
	 */
	@Override
	public String saveGZ(AccountModel accountModel) {
		if (accountModel.getBusinessType() == BusinessTypeEnum.BUSINESS_TYPE_DWZF.getValue()) {
			accountModel.setBusinessDetail("被" + accountModel.getAccountName() + "代付");

			UnitAccountModel unitAccountModel = new UnitAccountModel();
			unitAccountModel.setGroupId(accountModel.getGroupId());
			unitAccountModel.setHotelId(accountModel.getHotelId());
			unitAccountModel.setHotelCode(accountModel.getHotelCode());
			unitAccountModel.setRoomOrderId(accountModel.getRoomOrderId());
			unitAccountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
			unitAccountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_DWDF.getValue());
			unitAccountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_DWDF.getName());
			if (Utils.isNotNull(accountModel.getRoomNo())) {
				unitAccountModel.setBusinessDetail("给" + accountModel.getRoomNo() + "代付");
			} else {
				unitAccountModel.setBusinessDetail("给临客代付");
			}
			unitAccountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
			unitAccountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
			unitAccountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QT.getValue());
			unitAccountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QT.getName());
			unitAccountModel.setCustomId(accountModel.getCustomAccNo());
			unitAccountModel.setCustomType(CustomersEnum.CUSTOMERS_XYDW.getValue());
			unitAccountModel.setMoney(accountModel.getMoney());
			unitAccountModel.setIsSettle(UnitAccountSettleEnum.FALSE.getValue());
			unitAccountModel.setCreateUserId(accountModel.getCreateUserId());

			if (unitAccountService.saveUnitAccount(unitAccountModel) <= 0) {
				throw new BusinessException(UnitAccountReturnCodeEnum.UNITACCOUNT_SAVE_FAILURE.getCode(),
						UnitAccountReturnCodeEnum.UNITACCOUNT_SAVE_FAILURE.getDesc());
			}
		} else if (accountModel.getBusinessType() == BusinessTypeEnum.BUSINESS_TYPE_ZJZF.getValue()) {
			accountModel.setBusinessDetail("被" + accountModel.getAccountName() + "代付");

			UnitAccountModel unitAccountModel = new UnitAccountModel();
			unitAccountModel.setGroupId(accountModel.getGroupId());
			unitAccountModel.setHotelId(accountModel.getHotelId());
			unitAccountModel.setHotelCode(accountModel.getHotelCode());
			unitAccountModel.setRoomOrderId(accountModel.getRoomOrderId());
			unitAccountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
			unitAccountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_ZJDF.getValue());
			unitAccountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_ZJDF.getName());
			if (Utils.isNotNull(accountModel.getRoomNo())) {
				unitAccountModel.setBusinessDetail("给" + accountModel.getRoomNo() + "代付");
			} else {
				unitAccountModel.setBusinessDetail("给临客代付");
			}
			unitAccountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
			unitAccountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
			unitAccountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QT.getValue());
			unitAccountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QT.getName());
			unitAccountModel.setCustomId(accountModel.getCustomAccNo());
			unitAccountModel.setCustomType(CustomersEnum.CUSTOMERS_ZJ.getValue());
			unitAccountModel.setMoney(accountModel.getMoney());
			unitAccountModel.setIsSettle(UnitAccountSettleEnum.FALSE.getValue());
			unitAccountModel.setCreateUserId(accountModel.getCreateUserId());

			if (unitAccountService.saveUnitAccount(unitAccountModel) <= 0) {
				throw new BusinessException(UnitAccountReturnCodeEnum.UNITACCOUNT_SAVE_FAILURE.getCode(),
						UnitAccountReturnCodeEnum.UNITACCOUNT_SAVE_FAILURE.getDesc());
			}
		} else if (accountModel.getBusinessType() == BusinessTypeEnum.BUSINESS_TYPE_TRZF.getValue()) {
			accountModel.setBusinessDetail("被" + accountModel.getRoomNoZF() + "代付");

			AccountModel accountModelZF = new AccountModel();
			accountModelZF.setGroupId(accountModel.getGroupId());
			accountModelZF.setHotelId(accountModel.getHotelId());
			accountModelZF.setHotelCode(accountModel.getHotelCode());
			accountModelZF.setRoomOrderId(accountModel.getRoomOrderIdZF());
			accountModelZF.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
			accountModelZF.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_GRDF.getValue());
			accountModelZF.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_GRDF.getName());
			accountModelZF.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QT.getValue());
			accountModelZF.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QT.getName());
			if (Utils.isNotNull(accountModel.getRoomNo())) {
				accountModelZF.setBusinessDetail("给" + accountModel.getRoomNo() + "代付");
			} else {
				accountModelZF.setBusinessDetail("给临客代付");
			}
			
			accountModelZF.setMoney(accountModel.getMoney());
			accountModelZF.setAmount(1);
			accountModelZF.setRoomGuestId(accountModel.getRoomGuestIdZF());
			accountModelZF.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
			accountModelZF.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
			accountModelZF.setCreateUserId(accountModel.getCreateUserId());
			if ( Utils.isNull(this.saveOrderAccount(accountModelZF))) {
				throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getCode(),
						AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getDesc());
			}
		}

		accountModel.setAccountStatus(AccountStatusEnum.STATUS_GZ.getValue());
		accountModel.setAccountStatusName(AccountStatusEnum.STATUS_GZ.getName());
		accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue());
		accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QT.getValue());
		accountModel.setBusinessTypeName(BusinessTypeEnum.getName(accountModel.getBusinessType()));
		accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QT.getName());
		accountModel.setAmount(1);
		return this.saveOrderAccount(accountModel);
	}

	/**
	 * 功能:退房接口 <br/>
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月9日 下午5:55:11<br/>
	 * 
	 * @param accountModel
	 * @return <br/>
	 * @see com.yuju.account.service.IAccountService#endSettle(com.yuju.account.model.AccountModel)
	 */
	@Override
	public int saveEndSettle(List<AccountModel> list,String empId) {
		int rows = 0;
		for (AccountModel accountModel : list) {
			if(accountModel.getAccountType() == AccountTypeEnum.ACCOUNT_TYPE_YS.getValue()){
				if(Utils.isNull(this.saveOrderAccount(accountModel))){
					throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getCode(),
							AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getDesc());
				}else{
					rows++;
				}
			}else{
				accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue());
				accountModel.setAccountName(AccountTypeEnum.ACCOUNT_TYPE_SS.getName());
				accountModel.setBusinessTypeName(BusinessTypeEnum.getName(accountModel.getBusinessType()));
				accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.getName(accountModel.getBusinessTypeDetail()));
				accountModel.setBusinessDetail(BusinessTypeDetailEnum.getName(accountModel.getBusinessTypeDetail())
						+ BusinessTypeEnum.getName(accountModel.getBusinessType()));
				accountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
				accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
				accountModel.setAmount(1);
				accountModel.setCreateUserId(empId);
				if(Utils.isNull(this.saveOrderAccount(accountModel))){
					throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getCode(),
							AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getDesc());
				}else{
					rows++;
				}
			}
		}
		return rows;
	}

	/**
	 * 功能:新增预订单冲调账务 <br/>
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月8日 下午5:40:12<br/>
	 * 
	 * @param accountModelUpdate
	 * @return <br/>
	 * @see com.yuju.account.service.IAccountService#insertReduce(com.yuju.account.model.AccountModel)
	 */
	@Override
	public String saveReduce(AccountModel accountModelUpdate) {
		if (this.updateReserveAccount(accountModelUpdate) <= 0) {
			throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_UPDATE_FAILURE.getCode(),
					AccountReturnCodeEnum.ACCOUNT_UPDATE_FAILURE.getDesc());
		}

		AccountModel accountModel = new AccountModel();
		accountModel.setHotelId(accountModelUpdate.getHotelId());
		accountModel.setGroupId(accountModelUpdate.getGroupId());
		accountModel.setReserveId(accountModelUpdate.getReserveId());
		accountModel.setAccountStatus(AccountStatusEnum.STATUS_DEL.getValue());
		accountModel.setAccountStatusName(AccountStatusEnum.STATUS_DEL.getName());
		accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue());
		accountModel.setBusinessType(accountModelUpdate.getBusinessType());
		accountModel.setBusinessTypeName(BusinessTypeEnum.getName(accountModelUpdate.getBusinessType()));
		accountModel.setBusinessTypeDetail(accountModelUpdate.getBusinessTypeDetail());
		accountModel
				.setBusinessTypeDetailName(BusinessTypeDetailEnum.getName(accountModelUpdate.getBusinessTypeDetail()));
		accountModel.setBusinessDetail("冲调" + accountModelUpdate.getAccountCode());
		accountModel.setMoney(accountModelUpdate.getMoney().multiply(new BigDecimal(-1)));
		accountModel.setAmount(1);
		accountModel.setAccountCode(codeGenService.generate(accountModel.getGroupId(), accountModel.getHotelId(),
				accountModelUpdate.getHotelCode(), BusinessTypeEnum.getType(accountModel.getBusinessType())));
		accountModel.setRemark(accountModelUpdate.getRemark());
		accountModelUpdate.setRemark(null);
		accountModel.setCreateUserId(accountModelUpdate.getCreateUserId());
		accountModel.setUpdateUserId(accountModelUpdate.getUpdateUserId());

		return this.saveGetId(accountModel);
	}

	/**
	 * 功能:新增哑房账 <br/>
	 * 
	 * 循环添加账务
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月11日 下午4:06:42<br/>
	 * 
	 * @param accounts
	 * @return <br/>
	 * @see com.yuju.account.service.IAccountService#saveDumpRoomAccount(java.util.ArrayList)
	 */
	@Override
	public int saveDumpRoomAccount(List<AccountModel> accounts,String orderId) {
		int i = 0;
		for (AccountModel accountModel : accounts) {
			accountModel.setRoomOrderId(orderId);
			if (accountModel.getAccountStatus() == AccountStatusEnum.STATUS_GZ.getValue()) {
				if (Utils.isNull(this.saveGZ(accountModel))) {
					throw new BusinessException(SettleAccountReturnCodeEnum.SETTLEACCOUNT_GZ_FAILURE.getCode(),
							SettleAccountReturnCodeEnum.SETTLEACCOUNT_GZ_FAILURE.getDesc());
				} else {
					i++;
				}
			} else {
				accountModel.setBusinessTypeName(BusinessTypeEnum.getName(accountModel.getBusinessType()));
				accountModel.setBusinessTypeDetailName(
						BusinessTypeDetailEnum.getName(accountModel.getBusinessTypeDetail()));
				accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
				if (Utils.isNull(this.saveOrderAccount(accountModel) )) {
					throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getCode(),
							AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getDesc());
				} else {
					i++;
				}
			}
		}

		return i;
	}

	/**
	 * 功能:新增noshow房费账务 <br/>
	 * 
	 * 循环添加账务
	 *
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月12日 下午1:41:29<br/>
	 * 
	 * @param accounts
	 * @return <br/>
	 * @see com.yuju.account.service.IAccountService#saveNoShow(java.util.List)
	 */
	@Override
	public int saveNoShow(List<AccountModel> accounts) {
		int i = 0;
		for (AccountModel accountModel : accounts) {
			String reserveId = null;
			if (Utils.isNull(reserveId)) {
				reserveId = accountModel.getReserveId();
			}
			if (accountModel.getAccountStatus() == AccountStatusEnum.STATUS_OK.getValue()) {
				accountModel.setAmount(1);
				accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue());
				accountModel.setBusinessTypeName(BusinessTypeEnum.getName(accountModel.getBusinessType()));
				if (Utils.isNotNull(accountModel.getBusinessTypeDetail())) {
					accountModel.setBusinessTypeDetailName(
							BusinessTypeDetailEnum.getName(accountModel.getBusinessTypeDetail()));
					accountModel.setBusinessDetail(BusinessTypeDetailEnum.getName(accountModel.getBusinessTypeDetail())
							+ BusinessTypeEnum.getName(accountModel.getBusinessType()));
				} else {
					accountModel.setBusinessDetail(BusinessTypeEnum.getName(accountModel.getBusinessType()));
				}

				accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
				if ( Utils.isNull(this.saveReserveAccount(accountModel))) {
					throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getCode(),
							AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getDesc());
				}else{
					i++;
				}
			} else if (accountModel.getAccountStatus() == AccountStatusEnum.STATUS_GZ.getValue()) {
				accountModel.setAmount(1);
				accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue());
				accountModel.setBusinessTypeName(BusinessTypeEnum.getName(accountModel.getBusinessType()));
				accountModel.setBusinessTypeDetailName(
						BusinessTypeDetailEnum.getName(accountModel.getBusinessTypeDetail()));
				accountModel.setAccountStatusName(AccountStatusEnum.STATUS_GZ.getName());
				if (accountModel.getBusinessType() == BusinessTypeEnum.BUSINESS_TYPE_DWZF.getValue()) {
					accountModel.setBusinessDetail("被" + accountModel.getAccountName() + "代付");

					UnitAccountModel unitAccountModel = new UnitAccountModel();
					unitAccountModel.setGroupId(accountModel.getGroupId());
					unitAccountModel.setHotelId(accountModel.getHotelId());
					unitAccountModel.setHotelCode(accountModel.getHotelCode());
					unitAccountModel.setReserveId(accountModel.getReserveId());
					unitAccountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
					unitAccountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_DWDF.getValue());
					unitAccountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_DWDF.getName());
					unitAccountModel.setBusinessDetail("给" + accountModel.getReserveName() + "代付");
					unitAccountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QT.getValue());
					unitAccountModel
							.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QT.getName());
					unitAccountModel.setCustomId(accountModel.getCustomAccNo());
					unitAccountModel.setCustomType(CustomersEnum.CUSTOMERS_XYDW.getValue());
					unitAccountModel.setMoney(accountModel.getMoney());
					unitAccountModel.setIsSettle(UnitAccountSettleEnum.FALSE.getValue());
					unitAccountModel.setCreateUserId(accountModel.getCreateUserId());

					if (unitAccountService.saveUnitAccount(unitAccountModel) < 1) {
						throw new BusinessException(UnitAccountReturnCodeEnum.UNITACCOUNT_SAVE_FAILURE.getCode(),
								UnitAccountReturnCodeEnum.UNITACCOUNT_SAVE_FAILURE.getDesc());
					}
				} else if (accountModel.getBusinessType() == BusinessTypeEnum.BUSINESS_TYPE_ZJZF.getValue()) {
					accountModel.setBusinessDetail("被" + accountModel.getAccountName() + "代付");

					UnitAccountModel unitAccountModel = new UnitAccountModel();
					unitAccountModel.setGroupId(accountModel.getGroupId());
					unitAccountModel.setHotelId(accountModel.getHotelId());
					unitAccountModel.setHotelCode(accountModel.getHotelCode());
					unitAccountModel.setReserveId(accountModel.getReserveId());
					unitAccountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
					unitAccountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_ZJDF.getValue());
					unitAccountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_ZJDF.getName());
					unitAccountModel.setBusinessDetail("给" + accountModel.getReserveName() + "代付");
					unitAccountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QT.getValue());
					unitAccountModel
							.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QT.getName());
					unitAccountModel.setCustomId(accountModel.getCustomAccNo());
					unitAccountModel.setCustomType(CustomersEnum.CUSTOMERS_ZJ.getValue());
					unitAccountModel.setMoney(accountModel.getMoney());
					unitAccountModel.setIsSettle(UnitAccountSettleEnum.FALSE.getValue());
					unitAccountModel.setCreateUserId(accountModel.getCreateUserId());

					if (unitAccountService.saveUnitAccount(unitAccountModel) < 1) {
						throw new BusinessException(UnitAccountReturnCodeEnum.UNITACCOUNT_SAVE_FAILURE.getCode(),
								UnitAccountReturnCodeEnum.UNITACCOUNT_SAVE_FAILURE.getDesc());
					}
				} else if (accountModel.getBusinessType() == BusinessTypeEnum.BUSINESS_TYPE_TRZF.getValue()) {
					accountModel.setBusinessDetail("被" + accountModel.getRoomNoZF() + "代付");

					AccountModel accountModelZF = new AccountModel();
					accountModelZF.setGroupId(accountModel.getGroupId());
					accountModelZF.setHotelId(accountModel.getHotelId());
					accountModelZF.setHotelCode(accountModel.getHotelCode());
					accountModelZF.setRoomOrderId(accountModel.getRoomOrderIdZF());
					accountModelZF.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
					accountModelZF.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_GRDF.getValue());
					accountModelZF.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_GRDF.getName());
					accountModelZF.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QT.getValue());
					accountModelZF.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QT.getName());
					accountModelZF.setBusinessDetail("给" + accountModel.getReserveName() + "代付");
					accountModelZF.setMoney(accountModel.getMoney());
					accountModelZF.setAmount(1);
					accountModelZF.setRoomGuestId(accountModel.getRoomGuestIdZF());
					accountModelZF.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
					accountModelZF.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
					accountModelZF.setCreateUserId(accountModel.getCreateUserId());

					if ( Utils.isNull(this.saveOrderAccount(accountModelZF))) {
						throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getCode(),
								AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getDesc());
					}
				}
				if ( Utils.isNull(this.saveReserveAccount(accountModel))) {
					throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getCode(),
							AccountReturnCodeEnum.ACCOUNT_SAVE_FAILURE.getDesc());
				}else{
					i++;
				}
			} else {
				throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_STATUS_NOMATCH.getCode(),
						AccountReturnCodeEnum.ACCOUNT_STATUS_NOMATCH.getDesc());
			}
		}
		
		return i;
	}

	/**
	 * 功能: 根据多个订单id查询账务 <br/>
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月18日 下午8:08:21<br/>
	 * 
	 * @param list
	 * @return <br/>
	 * @see com.yuju.account.service.IAccountService#listByOrderIds(java.util.List)
	 */
	@Override
	public List<AccountModel> listByOrderIds(List<String> list) {
		
		List<AccountModel> accountList = accountDao.listByOrderIds(list);
		
		for (String id : list) {
			//获得订单信息
			RoomOrderModel roomOrderModel = accountDao.getRoomOrder(id);
			//获得订单入住标准
			CheckinStandardModel checkinStandardModel = checkinStandardService.findById(roomOrderModel.getCheckinStandardId());
			//判断是否在住
			if(roomOrderModel.getCheckinStatus() == RoomOrderStatusEnum.CHECKIN.getValue()){
				//判断入住类型
				if (roomOrderModel.getCheckinType() == CheckinTypeEnum.CHECKIN_ZD.getValue()){
					Date checkinTime = roomOrderModel.getCheckinTime();
					Date start = new Date(checkinTime.getTime() + (long)checkinStandardModel.getCheckinFullMinute() * 60 * 1000);
					Date end = new Date(
							checkinTime.getTime() + (long) checkinStandardModel.getHourCount() * 60 * 60 * 1000
									+ (long) checkinStandardModel.getCheckoutFullMinute() * 60 * 1000);
					Date now = new Date();
					//新增入住缓冲临时账务
					if(now.compareTo(start) == 1 && now.compareTo(end) <= 0 ){
						//构建临时账务
						AccountModel accountModel = new AccountModel();
						accountModel.setGroupId(roomOrderModel.getGroupId());
						accountModel.setHotelId(roomOrderModel.getHotelId());
						accountModel.setRoomOrderId(roomOrderModel.getId());
						accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
						accountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getValue());
						accountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getName());
						accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_ZDF.getValue());
						accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_ZDF.getName());
						accountModel.setBusinessDetail(accountModel.getBusinessTypeName() + accountModel.getBusinessTypeDetailName());
						accountModel.setMoney(roomOrderModel.getRoomPrice());
						accountModel.setAmount(1);
						accountModel.setRoomGuestId(roomOrderModel.getMainGuestId());
						accountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
						accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
						accountModel.setCreateUserId("SYSTEM");
						accountModel.setCreateUserName("SYSTEM");
						accountModel.setGmtCreate(now);
						accountModel.setGmtModified(now);
						accountModel.setRoomNo(roomOrderModel.getRoomCode());
						//添加临时账务
						accountList.add(0, accountModel);
					}else if(now.compareTo(end) == 1){ //新增超时临时账务
						//构建临时账务
						AccountModel accountModel = new AccountModel();
						accountModel.setGroupId(roomOrderModel.getGroupId());
						accountModel.setHotelId(roomOrderModel.getHotelId());
						accountModel.setRoomOrderId(roomOrderModel.getId());
						accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
						accountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getValue());
						accountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getName());
						accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getValue());
						accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getName());
						accountModel.setBusinessDetail(accountModel.getBusinessTypeName() + accountModel.getBusinessTypeDetailName());
						//获得房型信息
						RoomTypeModel roomTypeModel = roomTypeService.findById(roomOrderModel.getRoomTypeId());
						//设置全日租房价
						accountModel.setMoney(roomTypeModel.getPrice());
						accountModel.setAmount(1);
						accountModel.setRoomGuestId(roomOrderModel.getMainGuestId());
						accountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
						accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
						accountModel.setCreateUserId("SYSTEM");
						accountModel.setCreateUserName("SYSTEM");
						accountModel.setGmtCreate(now);
						accountModel.setGmtModified(now);
						accountModel.setRoomNo(roomOrderModel.getRoomCode());
						//添加临时账务
						accountList.add(0, accountModel);
					}
				}else if(roomOrderModel.getCheckinType() == CheckinTypeEnum.CHECKIN_TS.getValue()){
					Date checkinTime = roomOrderModel.getCheckinTime();
					Time checkoutTime = checkinStandardModel.getCheckoutTime();
					Date now = new Date();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
					Time nowTime = null;
					Date checkoutTimeToday = null;
					Date firstTimeYS = null; 
					try {
						nowTime = new Time(sdfTime.parse(sdfTime.format(now)).getTime());
						checkoutTimeToday = new Date(sdf.parse(sdf.format(checkinTime)).getTime() + checkoutTime.getTime() +(long)8 * 60 * 60 * 1000);
						firstTimeYS = new Date(sdf.parse(sdf.format(checkinTime)).getTime()+(long)24 * 60 * 60 * 1000);
					} catch (ParseException e) {
						e.printStackTrace();
					}
					// 入住缓冲临时账务
					if (now.compareTo(firstTimeYS) < 0 && now.compareTo(new Date(checkinTime.getTime()
							+ (long) checkinStandardModel.getCheckinFullMinute() * 60 * 1000)) == 1) {
						// 构建临时账务
						AccountModel accountModel = new AccountModel();
						accountModel.setGroupId(roomOrderModel.getGroupId());
						accountModel.setHotelId(roomOrderModel.getHotelId());
						accountModel.setRoomOrderId(roomOrderModel.getId());
						accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
						accountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getValue());
						accountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getName());
						accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getValue());
						accountModel
								.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getName());
						accountModel.setBusinessDetail(
								accountModel.getBusinessTypeName() + accountModel.getBusinessTypeDetailName());
						accountModel.setMoney(roomOrderModel.getRoomPrice());
						accountModel.setAmount(1);
						accountModel.setRoomGuestId(roomOrderModel.getMainGuestId());
						accountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
						accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
						accountModel.setCreateUserId("SYSTEM");
						accountModel.setCreateUserName("SYSTEM");
						accountModel.setGmtCreate(now);
						accountModel.setGmtModified(now);
						accountModel.setRoomNo(roomOrderModel.getRoomCode());
						// 添加临时账务
						accountList.add(0, accountModel);
					}
					//超时临时账务
					if(checkinTime.compareTo(checkoutTimeToday) == -1){
						if (nowTime
								.compareTo(new Time(checkoutTime.getTime()
										+ (long) checkinStandardModel.getCheckoutHalfMinute() * 60 * 1000)) == 1
								&& nowTime.compareTo(new Time(checkoutTime.getTime()
										+ (long) checkinStandardModel.getCheckoutFullMinute() * 60 * 1000)) < 0) {
							//构建临时账务
							AccountModel accountModel = new AccountModel();
							accountModel.setGroupId(roomOrderModel.getGroupId());
							accountModel.setHotelId(roomOrderModel.getHotelId());
							accountModel.setRoomOrderId(roomOrderModel.getId());
							accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
							accountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getValue());
							accountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getName());
							accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_BRZ.getValue());
							accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_BRZ.getName());
							accountModel.setBusinessDetail(accountModel.getBusinessTypeName() + accountModel.getBusinessTypeDetailName());
							accountModel.setMoney(roomOrderModel.getRoomPrice().divide(new BigDecimal("2"), 2, BigDecimal.ROUND_HALF_UP));
							accountModel.setAmount(1);
							accountModel.setRoomGuestId(roomOrderModel.getMainGuestId());
							accountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
							accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
							accountModel.setCreateUserId("SYSTEM");
							accountModel.setCreateUserName("SYSTEM");
							accountModel.setGmtCreate(now);
							accountModel.setGmtModified(now);
							accountModel.setRoomNo(roomOrderModel.getRoomCode());
							//添加临时账务
							accountList.add(0, accountModel);
						}else if(nowTime.compareTo(new Time(checkoutTime.getTime()
								+ (long) checkinStandardModel.getCheckoutFullMinute() * 60 * 1000)) == 1){
							//构建临时账务
							AccountModel accountModel = new AccountModel();
							accountModel.setGroupId(roomOrderModel.getGroupId());
							accountModel.setHotelId(roomOrderModel.getHotelId());
							accountModel.setRoomOrderId(roomOrderModel.getId());
							accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
							accountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getValue());
							accountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getName());
							accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getValue());
							accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getName());
							accountModel.setBusinessDetail(accountModel.getBusinessTypeName() + accountModel.getBusinessTypeDetailName());
							//获得房型信息
							RoomTypeModel roomTypeModel = roomTypeService.findById(roomOrderModel.getRoomTypeId());
							//设置全日租房价
							accountModel.setMoney(roomTypeModel.getPrice());
							accountModel.setAmount(1);
							accountModel.setRoomGuestId(roomOrderModel.getMainGuestId());
							accountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
							accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
							accountModel.setCreateUserId("SYSTEM");
							accountModel.setCreateUserName("SYSTEM");
							accountModel.setGmtCreate(now);
							accountModel.setGmtModified(now);
							accountModel.setRoomNo(roomOrderModel.getRoomCode());
							//添加临时账务
							accountList.add(0, accountModel);
						}
					}else if(now.compareTo(firstTimeYS) == 1){
						if (nowTime
								.compareTo(new Time(checkoutTime.getTime()
										+ (long) checkinStandardModel.getCheckoutHalfMinute() * 60 * 1000)) == 1
								&& nowTime.compareTo(new Time(checkoutTime.getTime()
										+ (long) checkinStandardModel.getCheckoutFullMinute() * 60 * 1000)) < 0) {
							//构建临时账务
							AccountModel accountModel = new AccountModel();
							accountModel.setGroupId(roomOrderModel.getGroupId());
							accountModel.setHotelId(roomOrderModel.getHotelId());
							accountModel.setRoomOrderId(roomOrderModel.getId());
							accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
							accountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getValue());
							accountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getName());
							accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_BRZ.getValue());
							accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_BRZ.getName());
							accountModel.setBusinessDetail(accountModel.getBusinessTypeName() + accountModel.getBusinessTypeDetailName());
							accountModel.setMoney(roomOrderModel.getRoomPrice().divide(new BigDecimal("2"), 2, BigDecimal.ROUND_HALF_UP));
							accountModel.setAmount(1);
							accountModel.setRoomGuestId(roomOrderModel.getMainGuestId());
							accountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
							accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
							accountModel.setCreateUserId("SYSTEM");
							accountModel.setCreateUserName("SYSTEM");
							accountModel.setGmtCreate(now);
							accountModel.setGmtModified(now);
							accountModel.setRoomNo(roomOrderModel.getRoomCode());
							//添加临时账务
							accountList.add(0, accountModel);
						}else if(nowTime.compareTo(new Time(checkoutTime.getTime()
								+ (long) checkinStandardModel.getCheckoutFullMinute() * 60 * 1000)) == 1){
							//构建临时账务
							AccountModel accountModel = new AccountModel();
							accountModel.setGroupId(roomOrderModel.getGroupId());
							accountModel.setHotelId(roomOrderModel.getHotelId());
							accountModel.setRoomOrderId(roomOrderModel.getId());
							accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
							accountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getValue());
							accountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getName());
							accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getValue());
							accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getName());
							accountModel.setBusinessDetail(accountModel.getBusinessTypeName() + accountModel.getBusinessTypeDetailName());
							//获得房型信息
							RoomTypeModel roomTypeModel = roomTypeService.findById(roomOrderModel.getRoomTypeId());
							//设置全日租房价
							accountModel.setMoney(roomTypeModel.getPrice());
							accountModel.setAmount(1);
							accountModel.setRoomGuestId(roomOrderModel.getMainGuestId());
							accountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
							accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
							accountModel.setCreateUserId("SYSTEM");
							accountModel.setCreateUserName("SYSTEM");
							accountModel.setGmtCreate(now);
							accountModel.setGmtModified(now);
							accountModel.setRoomNo(roomOrderModel.getRoomCode());
							//添加临时账务
							accountList.add(0, accountModel);
						}
					}
				}else if(roomOrderModel.getCheckinType() == CheckinTypeEnum.CHECKIN_QT.getValue()){
					Date checkinTime = roomOrderModel.getCheckinTime();
					Time checkoutTime = checkinStandardModel.getCheckoutTime();
					Date now = new Date();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
					Time nowTime = null;
					Date firstTimeYS = null;
					try {
						nowTime = new Time(sdfTime.parse(sdfTime.format(now)).getTime());
						firstTimeYS = new Date(sdf.parse(sdf.format(checkinTime)).getTime()+(long)24 * 60 * 60 * 1000);
					} catch (ParseException e) {
						e.printStackTrace();
					}
					//入住缓冲临时账务
					if(now.compareTo(new Date(checkinTime.getTime() + (long) checkinStandardModel.getCheckinFullMinute() * 60 * 1000)) == 1
							&& now.compareTo(firstTimeYS)<0){
						//构建临时账务
						AccountModel accountModel = new AccountModel();
						accountModel.setGroupId(roomOrderModel.getGroupId());
						accountModel.setHotelId(roomOrderModel.getHotelId());
						accountModel.setRoomOrderId(roomOrderModel.getId());
						accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
						accountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getValue());
						accountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getName());
						accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getValue());
						accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getName());
						accountModel.setBusinessDetail(accountModel.getBusinessTypeName() + accountModel.getBusinessTypeDetailName());
						accountModel.setMoney(roomOrderModel.getRoomPrice());
						accountModel.setAmount(1);
						accountModel.setRoomGuestId(roomOrderModel.getMainGuestId());
						accountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
						accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
						accountModel.setCreateUserId("SYSTEM");
						accountModel.setCreateUserName("SYSTEM");
						accountModel.setGmtCreate(now);
						accountModel.setGmtModified(now);
						accountModel.setRoomNo(roomOrderModel.getRoomCode());
						//添加临时账务
						accountList.add(0, accountModel);
					}
					//超时临时账务
					if(now.compareTo(firstTimeYS) == 1){
						if (nowTime
								.compareTo(new Time(checkoutTime.getTime()
										+ (long) checkinStandardModel.getCheckoutHalfMinute() * 60 * 1000)) == 1
								&& nowTime.compareTo(checkinStandardModel.getCheckoutOverTime()) < 0) {
							//构建临时账务
							AccountModel accountModel = new AccountModel();
							accountModel.setGroupId(roomOrderModel.getGroupId());
							accountModel.setHotelId(roomOrderModel.getHotelId());
							accountModel.setRoomOrderId(roomOrderModel.getId());
							accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
							accountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getValue());
							accountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getName());
							accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_BRZ.getValue());
							accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_BRZ.getName());
							accountModel.setBusinessDetail(accountModel.getBusinessTypeName() + accountModel.getBusinessTypeDetailName());
							accountModel.setMoney(roomOrderModel.getRoomPrice().divide(new BigDecimal("2"), 2, BigDecimal.ROUND_HALF_UP));
							accountModel.setAmount(1);
							accountModel.setRoomGuestId(roomOrderModel.getMainGuestId());
							accountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
							accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
							accountModel.setCreateUserId("SYSTEM");
							accountModel.setCreateUserName("SYSTEM");
							accountModel.setGmtCreate(now);
							accountModel.setGmtModified(now);
							accountModel.setRoomNo(roomOrderModel.getRoomCode());
							//添加临时账务
							accountList.add(0, accountModel);
						}else if(nowTime.compareTo(checkinStandardModel.getCheckoutOverTime()) == 1){
							//构建临时账务
							AccountModel accountModel = new AccountModel();
							accountModel.setGroupId(roomOrderModel.getGroupId());
							accountModel.setHotelId(roomOrderModel.getHotelId());
							accountModel.setRoomOrderId(roomOrderModel.getId());
							accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
							accountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getValue());
							accountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getName());
							accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getValue());
							accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getName());
							accountModel.setBusinessDetail(accountModel.getBusinessTypeName() + accountModel.getBusinessTypeDetailName());
							accountModel.setMoney(roomOrderModel.getRoomPrice());
							accountModel.setAmount(1);
							accountModel.setRoomGuestId(roomOrderModel.getMainGuestId());
							accountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
							accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
							accountModel.setCreateUserId("SYSTEM");
							accountModel.setCreateUserName("SYSTEM");
							accountModel.setGmtCreate(now);
							accountModel.setGmtModified(now);
							accountModel.setRoomNo(roomOrderModel.getRoomCode());
							//添加临时账务
							accountList.add(0, accountModel);
						}
					}
				}else if(roomOrderModel.getCheckinType() == CheckinTypeEnum.CHECKIN_GY.getValue()){
					Date checkinTime = roomOrderModel.getCheckinTime();
					Date now = new Date();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
					Time nowTime = null;
					Date firstTimeYS = null;
					try {
						nowTime = new Time(sdfTime.parse(sdfTime.format(now)).getTime());
						firstTimeYS = new Date(sdf.parse(sdf.format(checkinTime)).getTime()+(long)24 * 60 * 60 * 1000);
					} catch (ParseException e) {
						e.printStackTrace();
					}
					//超时临时账务
					if (now.compareTo(firstTimeYS) == 1 && nowTime.compareTo(checkinStandardModel.getCheckoutOverTime()) == 1) {
						
						//构建临时账务
						AccountModel accountModel = new AccountModel();
						accountModel.setGroupId(roomOrderModel.getGroupId());
						accountModel.setHotelId(roomOrderModel.getHotelId());
						accountModel.setRoomOrderId(roomOrderModel.getId());
						accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
						accountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getValue());
						accountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getName());
						accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getValue());
						accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getName());
						accountModel.setBusinessDetail(accountModel.getBusinessTypeName() + accountModel.getBusinessTypeDetailName());
						accountModel.setMoney(roomOrderModel.getRoomPrice());
						accountModel.setAmount(1);
						accountModel.setRoomGuestId(roomOrderModel.getMainGuestId());
						accountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
						accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
						accountModel.setCreateUserId("SYSTEM");
						accountModel.setCreateUserName("SYSTEM");
						accountModel.setGmtCreate(now);
						accountModel.setGmtModified(now);
						accountModel.setRoomNo(roomOrderModel.getRoomCode());
						//添加临时账务
						accountList.add(0, accountModel);
					}
					
				}
				
			}
		}
		return accountList;
	}

	/**
	 * 功能:查找预订单账务并总结 <br/>
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月19日 上午11:22:59<br/>
	 * 
	 * @param accountModel
	 * @return <br/>
	 * @see com.yuju.account.service.IAccountService#listByReserveId(com.yuju.model.account.AccountModel)
	 */
	@Override
	public Map<String, Object> listByReserveId(AccountModel accountModel) {
		Map<String, Object> map = new HashMap<>();
		accountModel.setHotelId(null);
		accountModel.setGroupId(null);
		List<AccountModel> accountList = list(accountModel);
		Map<String, Object> sumMap = this.countAccounts(accountList);
		map.put("accountList", accountList);
		map.put("sumMap", sumMap);
		return map;
	}
	
	/**
	 * 功能：预订单账务总结<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月19日 下午4:52:31 <br/>
	 */
	public Map<String, Object> countAccounts(List<AccountModel> accountList){
		Map<String, Object> map = new HashMap<>();
		BigDecimal sumXJ = new BigDecimal(0);
		BigDecimal sumSK = new BigDecimal(0);
		BigDecimal sumZZ = new BigDecimal(0);
		BigDecimal sumWX = new BigDecimal(0);
		BigDecimal sumZFB = new BigDecimal(0);
		BigDecimal sumTK = new BigDecimal(0);
		BigDecimal sumDJ = new BigDecimal(0);
		for (AccountModel accountModel : accountList) {
			if(accountModel.getBusinessType() == BusinessTypeEnum.BUSINESS_TYPE_TK.getValue()){
				sumTK = accountModel.getMoney().add(sumTK);
			}else if(accountModel.getBusinessType() == BusinessTypeEnum.BUSINESS_TYPE_DJ.getValue()){
				if(accountModel.getBusinessTypeDetail() == BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_XJ.getValue()){
					sumXJ = accountModel.getMoney().add(sumXJ);
				}else if(accountModel.getBusinessTypeDetail() == BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_SK.getValue()){
					sumSK = accountModel.getMoney().add(sumSK);
				}else if(accountModel.getBusinessTypeDetail() == BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_ZZ.getValue()){
					sumZZ = accountModel.getMoney().add(sumZZ);
				}else if(accountModel.getBusinessTypeDetail() == BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_WX.getValue()){
					sumWX = accountModel.getMoney().add(sumWX);
				}else{
					sumZFB = accountModel.getMoney().add(sumZFB);
				}
			}
		}
		sumDJ = sumXJ.add(sumSK).add(sumZZ).add(sumWX).add(sumZFB).subtract(sumTK);
		map.put("sumDJ", sumDJ);
		map.put("sumXJ", sumXJ);
		map.put("sumSK", sumSK);
		map.put("sumZZ", sumZZ);
		map.put("sumWX", sumWX);
		map.put("sumZFB", sumZFB);
		map.put("sumTK", sumTK);
		return map;
	}

	/**
	 * 功能:客账查询 <br/>
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月19日 下午2:32:43<br/>
	 * 
	 * @param accountModel
	 * @return <br/>
	 * @see com.yuju.account.service.IAccountService#listOrderAccount(com.yuju.model.account.AccountModel)
	 */
	@Override
	public PageModel listOrderAccount(AccountModel accountModel, Integer currentPage, Integer rows) {
		return accountDao.listOrderAccount(accountModel, currentPage, rows);
	}

	@Override
	public OrderAccountStatModel statOrderAccount(List<AccountModel> list){
		OrderAccountStatModel result = new OrderAccountStatModel();
		if (list == null || list.size() == 0) {
			return result;
		}

		for (AccountModel item: list) {
			Byte accountType = item.getAccountType();
			Byte businessType = item.getBusinessType();
			Byte businessTypeDetail = item.getBusinessTypeDetail();
			BigDecimal money = item.getMoney();
			Byte accountStatus = item.getAccountStatus();

			if (accountType == null || businessType == null
					|| businessTypeDetail == null || money == null || accountStatus == null) {
				throw new BusinessException(AccountReturnCodeEnum.ACCOUNT_ATTRIBUTE_NULL_ERR.getCode(),
						AccountReturnCodeEnum.ACCOUNT_ATTRIBUTE_NULL_ERR.getDesc());
			}

			//应收
			if (accountType.equals(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue())) {
				if (businessType.equals(BusinessTypeEnum.BUSINESS_TYPE_YSFF.getValue())
						|| businessType.equals(BusinessTypeEnum.BUSINESS_TYPE_JSFF.getValue())
						|| businessType.equals(BusinessTypeEnum.BUSINESS_TYPE_XTJF.getValue())){
					result.setYsFfSum(money.add(result.getYsFfSum()));
				}
				else if (businessType.equals(BusinessTypeEnum.BUSINESS_TYPE_XSD.getValue())) {
					result.setYsXsdSum(money.add(result.getYsXsdSum()));
				}
				else if (businessType.equals(BusinessTypeEnum.BUSINESS_TYPE_GRDF.getValue())) {
					result.setYsGrdfSum(money.add(result.getYsGrdfSum()));
				}
				else {
					result.setYs3rdSum(money.add(result.getYs3rdSum()));
				}
			}
			//实收
			else if (accountType.equals(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue())) {

				//只有退款时需要将金额变为负数
				if (businessType.equals(BusinessTypeEnum.BUSINESS_TYPE_TK.getValue())) {
					money = BigDecimal.ZERO.subtract(money);
				}

				if (businessTypeDetail.equals(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_XJ.getValue())) {
					result.setSsCashSum(money.add(result.getSsCashSum()));
				}
				else if (businessTypeDetail.equals(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_SK.getValue())) {
					result.setSsSkSum(money.add(result.getSsSkSum()));
				}
				else if (businessTypeDetail.equals(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_ZZ.getValue())) {
					result.setSsZzSum(money.add(result.getSsZzSum()));
				}
				else if (businessTypeDetail.equals(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_WX.getValue())) {
					result.setSsWxSum(money.add(result.getSsWxSum()));
				}
				else if (businessTypeDetail.equals(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_ZFB.getValue())) {
					result.setSsZfbSum(money.add(result.getSsZfbSum()));
				}
				else {
					result.setSsOtherSum(money.add(result.getSsOtherSum()));
				}
			}
		}

		//统计
		BigDecimal sum = BigDecimal.ZERO;

		//收款合计
		sum = result.getSsSum();
		sum = sum.add(result.getSsCashSum());
		sum = sum.add(result.getSsSkSum());
		sum = sum.add(result.getSsZzSum());
		sum = sum.add(result.getSsWxSum());
		sum = sum.add(result.getSsZfbSum());
		sum = sum.add(result.getSsOtherSum());
		result.setSsSum(sum);

		//消费合计
		sum = result.getYsXfSum();
		sum = sum.add(result.getYsFfSum());
		sum = sum.add(result.getYsXsdSum());
		result.setYsXfSum(sum);

		//其他应收
		sum = result.getYsOtherSum();
		sum = sum.add(result.getYs3rdSum());
		sum = sum.add(result.getYsGrdfSum());
		result.setYsOtherSum(sum);

		//合计
		sum = result.getSum();
		sum = sum.add(result.getSsSum());
		sum = sum.subtract(result.getYsXfSum());
		sum = sum.subtract(result.getYsOtherSum());
		result.setSum(sum);

		return result;
	}

	/**
	 * 功能:根据orderId查找账务统计信息 <br/>
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月21日 下午5:44:04<br/>
	 * 
	 * @param orderId
	 * @return <br/>
	 * @see com.yuju.account.service.IAccountService#getDetailByOrderId(java.lang.String)
	 */
	@Override
	public Map<String, Object> getDetailByOrderId(String orderId) {
		AccountModel accountModel = new AccountModel();
		accountModel.setRoomOrderId(orderId);
		List<AccountModel> list = this.list(accountModel);
		OrderAccountStatModel orderAccountStatModel = this.statOrderAccount(list);
		BigDecimal ssSum = orderAccountStatModel.getSsSum();
		BigDecimal ysSum = orderAccountStatModel.getYsXfSum().add(orderAccountStatModel.getYsOtherSum());
		Map<String, Object> map = new HashMap<>();
		//总收款
		map.put("ssSum", ssSum);
		//总应收
		map.put("ysSum", ysSum);
		return map;
	}

	/**
	 * 功能:新增预订账务 <br/>
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2018年1月16日 上午10:10:28<br/>
	 * 
	 * @param groupId
	 * @param hotelId
	 * @param reserveId
	 * @param money
	 * @param businessTypeDetail
	 * @param remark
	 * @return <br/>
	 * @see com.yuju.account.service.IAccountService#saveReserveDepositAccount(java.lang.String, java.lang.String, java.lang.String, java.math.BigDecimal, java.lang.Byte, java.lang.String)
	 */
	@Override
	public int saveReserveDepositAccount(String groupId, String hotelId,String hotelCode, String reserveId, BigDecimal money,
			Byte businessTypeDetail,String remark) {
		AccountModel accountModel = new AccountModel();
		accountModel.setGroupId(groupId);
		accountModel.setHotelId(hotelId);
		accountModel.setReserveId(reserveId);
		accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue());
		accountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_DJ.getValue());
		accountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_DJ.getName());
		accountModel.setBusinessTypeDetail(businessTypeDetail);
		accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.getName(businessTypeDetail));
		accountModel.setBusinessDetail(accountModel.getBusinessTypeDetailName()+accountModel.getBusinessTypeName());
		accountModel.setMoney(money);
		accountModel.setAmount(1);
		accountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
		accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
		accountModel.setRemark(remark);
		accountModel.setHotelCode(hotelCode);
		accountModel.setCreateUserId("SYSTEM");
		accountModel.setAccountCode(codeGenService.generate(accountModel.getGroupId(), accountModel.getHotelId(),
				accountModel.getHotelCode(),BusinessTypeEnum.getType(accountModel.getBusinessType()) ));
		if(Utils.isNull(this.saveGetId(accountModel))){
			return 0;
		}
		return 1;
	}

	@Override
	public List<AccountModel> listOrderAccountNoPage(AccountModel accountModel) {
		return accountDao.listOrderAccountNoPage(accountModel);
	}

	@Override
	public int getAccountAmountSum(AccountModel accountModel) {
		return accountDao.getAccountAmountSum(accountModel);
	}

	@Override
	public List<AccountModel> getCTReport(AccountModel accountModel) {
		return accountDao.getCTReport(accountModel);
	}

	@Override
	public PageModel getCTReportPage(AccountModel accountModel, Integer currentPage, Integer rows) {
		return accountDao.getCTReportPage(accountModel, currentPage, rows);
	}

	@Override
	public BigDecimal getCTMoneySum(AccountModel accountModel) {
		return accountDao.getCTMoneySum(accountModel);
	}


	@Override
	public byte[] expBusinessDailyRpt(AccountModel accountModel) throws IOException {
		ExcelExp e = new ExcelExp();
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		e.createRow(0);
		e.setCell(0, "房号");
		e.setCell(1, "姓名");
		e.setCell(2, "业务类型");
		e.setCell(3, "数量");
		e.setCell(4, "金额");
		e.setCell(5, "消费时间");
		e.setCell(6, "班次号");
		e.setCell(7, "操作员");
		e.setColumnWidth(0, 3000);
		e.setColumnWidth(1, 3000);
		e.setColumnWidth(2, 3000);
		e.setColumnWidth(3, 2000);
		e.setColumnWidth(4, 3000);
		e.setColumnWidth(5, 5000);
		e.setColumnWidth(6, 5000);
		e.setColumnWidth(7, 3000);
		
		Integer amount = accountModel.getAmount();
		BigDecimal money = accountModel.getMoney();
		accountModel.setMoney(null);
		accountModel.setAmount(null);
		List<AccountModel> list = this.listOrderAccountNoPage(accountModel);
		if(Utils.isNull(list)){
			throw new BusinessException(ReturnCodeEnum.NO_DATA.getCode(), ReturnCodeEnum.NO_DATA.getDesc());
		}
		for (int i = 0; i < list.size(); i++) {
			e.createRow(i+1);
			e.setCell(0, list.get(i).getRoomNo());
			e.setCell(1, list.get(i).getGuestName());
			e.setCell(2, list.get(i).getBusinessTypeName());
			e.setCell(3, list.get(i).getAmount());
			e.setCell(4, list.get(i).getMoney().toString());
			e.setCell(5, DateUtil.format(list.get(i).getGmtCreate()));
			e.setCell(6, list.get(i).getShiftCode());
			e.setCell(7, list.get(i).getCreateUserName());
		}
		e.createRow(list.size()+1);
		e.setCell(0, "合计");
		e.setCell(3, amount);
		e.setCell(4, money.toString());
		e.getWorkbook().write(os);
		os.flush();
		return os.toByteArray();
	}


	@Override
	public byte[] expCTAccountRpt(AccountModel accountModel) throws IOException {
		ExcelExp e = new ExcelExp();
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		e.createRow(0);
		e.setCell(0, "冲调时间");
		e.setCell(1, "类型");
		e.setCell(2, "名称");
		e.setCell(3, "房间号");
		e.setCell(4, "营业项目");
		e.setCell(5, "金额");
		e.setCell(6, "操作员");
		e.setCell(7, "原账务操作员");
		e.setCell(8, "冲调原因");
		e.setColumnWidth(0, 5000);
		e.setColumnWidth(1, 4000);
		e.setColumnWidth(2, 3000);
		e.setColumnWidth(3, 3000);
		e.setColumnWidth(4, 3000);
		e.setColumnWidth(5, 3000);
		e.setColumnWidth(6, 3000);
		e.setColumnWidth(7, 3000);
		e.setColumnWidth(8, 8000);
		
		List<AccountModel> list = this.getCTReport(accountModel);
		BigDecimal sum = this.getCTMoneySum(accountModel);
		if(Utils.isNull(sum) || Utils.isNull(list)){
			throw new BusinessException(ReturnCodeEnum.NO_DATA.getCode(), ReturnCodeEnum.NO_DATA.getDesc());
		}
		for (int i = 0; i < list.size(); i++) {
			e.createRow(i+1);
			e.setCell(0, DateUtil.format(list.get(i).getGmtCreate()));
			e.setCell(1, list.get(i).getType());
			e.setCell(2, list.get(i).getGuestName());
			e.setCell(3, list.get(i).getRoomNo());
			e.setCell(4, list.get(i).getBusinessTypeName());
			e.setCell(5, list.get(i).getMoney().toString());
			e.setCell(6, list.get(i).getUpdateUserName());
			e.setCell(7, list.get(i).getCreateUserName());
			e.setCell(8, list.get(i).getRemark());
		}
		e.createRow(list.size()+1);
		e.setCell(0, "合计");
		e.setCell(5, sum.toString());
		e.getWorkbook().write(os);
		os.flush();
		return os.toByteArray();
	}
	
}
